<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 增减购物车信息
 *
 * @author      Eden
 * @datetime    2016/12/18 23:18
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Lists2_Model extends API_Model
{
    private $width = 320;
    private $height = 320;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主程序
     * @param array $params
     */
    public function run($params = array())
    {
        $send['state'] = false;
        do {

            if (empty($params['userId'])) {
                $send['error'] = '参数错误';
                break;
            }

            $res = $this->getLists($params['userId']);
            $send['state'] = true;
            $send['data'] = empty($res) ? [] : $res;

        } while(false);
        $this->output->send($send);
    }

    public function getLists($userId = 0)
    {
        $db = clone($this->db);
        $q = $db->select('g.id as goodsId, g.goodsName, g.thumb, g.bannerThumb, g.liveUrl, g.expressWeight, g.created, c.num, c.ruleId, r.name, r.oldPrice, r.salePrice, r.purchaserPrice')
            ->from('user_car as c')
            ->join('base_goods as g', 'g.id = c.goodsId')
            ->join('base_goods_rule as r', 'r.id=c.ruleId')
            ->order_by('c.created', 'DESC')
            ->where('c.userId', $userId)
            ->get();
        $rows = $q->result_array();

        if (empty($rows)) {
            return [];
        }

        $userType = $this->getUserType($userId);

        $returns = array();
        foreach($rows as $item) {
            $thumb = !empty($item['bannerThumb']) ? $item['bannerThumb'] : $item['thumb'];
            $returns[] = array(
                'goodsId' => intval($item['goodsId']),
                'ruleId' => intval($item['ruleId']),
                'goodsName' => $item['goodsName'],
                'thumb' => thumb($thumb, $this->width, $this->height),
                'liveUrl' => value($item, 'liveUrl', ''),
                'expressWeight' => doubleval($item['expressWeight']),
                'price' => $userType == 1 ? money_format('%i', $item['purchaserPrice'] / 100) : money_format('%i', $item['salePrice'] / 100),
                'num' => intval($item['num'])
            );
        }

        return $returns;
    }

}

